import Vue from 'vue';
import Router from 'vue-router';
import envConfig from '../config/env';

// 防止重复this.$router.push()同一个路由时报错
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.use(Router);

const routerMap = [
  {
    name: 'user-login',
    path: '/user/login',
    component: () => import('@/views/user/Login'),
    meta: {title: '用户登录'}
  },
  {
    name: 'home',
    path: '/',
    redirect: '/dashboard',
    component: () => import('@/views/common/Layout'),
    children: [
      {
        name: 'dashboard',
        path: '/dashboard',
        component: () => import('@/views/dashboard'),
        meta: {title: '数据看板', breadcrumb: ['数据看板']}
      },
      {
        name: 'workflow-definitions',
        path: '/workflow/definitions',
        component: () => import('@/views/workflow/Definitions'),
        meta: {title: '流程管理 - 流程定义列表', breadcrumb: ['流程管理', '流程定义列表']}
      },
      {
        name: 'system-users',
        path: '/system/users',
        component: () => import('@/views/system/Users'),
        meta: {title: '系统管理 - 用户列表', breadcrumb: ['系统管理', '用户列表']}
      },
      {
        name: 'system-roles',
        path: '/system/roles',
        component: () => import('@/views/system/Roles'),
        meta: {title: '系统管理 - 角色列表', breadcrumb: ['系统管理', '角色列表']}
      },
      {
        name: 'system-authorities',
        path: '/system/authorities',
        component: () => import('@/views/system/Authorities'),
        meta: {title: '系统管理 - 权限列表', breadcrumb: ['系统管理', '权限列表']}
      },
      {
        name: 'test-menu-01',
        path: '/test/menu-01',
        component: () => import('@/views/test/Test01'),
        meta: {title: '多级菜单 - 菜单一', breadcrumb: ['多级菜单', '菜单一']}
      },
      {
        name: 'test-menu-03',
        path: '/test/menu-03',
        component: () => import('@/views/test/Test03'),
        meta: {title: '多级菜单 - 菜单三', breadcrumb: ['多级菜单', '菜单三']}
      }
    ]
  }
];

const router = new Router({
  base: envConfig.baseRouter,
  mode: 'history',
  routes: routerMap
});

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router;